<?php

namespace Admin\Modules;

use Hirudo\Core\Module;

/**
 * Description of Plugins
 *
 * @author Elkin José
 */
class Plugins extends Module {

    public function index() {

        $plugins = $this->component("Plugins")->getAll();

        $this->assign("plugins", $plugins);
        $this->assign("editContent", $this->route->action("editContent"));

        $this->display();
    }

    public function editContent() {

        $idPlugin = $this->request->get("idPlugin");

        $medias = $this->component("Media")->getByPlugin($idPlugin);

        $this->assign("medias", $medias);
        $this->assign("index", $this->route->action("index"));
        $this->assign("action", $this->route->action("addContent"));
        $this->assign("delete", $this->route->action("deleteContent"));

        $this->display();
    }

    public function addContent() {

        $idPlugin = $this->request->get("idPlugin");
        $fileName = $this->request->post("file_name");
        $fileTitle = $this->request->post("file_title");
        $fileSecTitle = $this->request->post("file_second_title");
        $file = $_FILES["file"]['name'];
        
        $formato = $this->extension($file);
        
        $destino = "../media/sliders/" . $fileName . "." . $formato;
        
        copy($_FILES['file']['tmp_name'], $destino);
        
        $fileInfo = array('idPlugin' => $idPlugin, 'name' => $fileName, 'format' => $formato, 'title' => $fileTitle, 'secondTitle' => $fileSecTitle);
        
        $medias = $this->component("Media")->add($fileInfo);
        
        $this->assign("index", $this->route->action("index"));
        
        $this->display();
    }
    
    function extension($filename){
        return substr(strrchr($filename, '.'), 1);
    }
    
    function deleteContent() {
        
        $idMedia = $this->request->get("idMedia");
        
        $media = $this->component("Media")->getById($idMedia);
        
        $file = "../media/sliders/".$media[0]["name"].".".$media[0]["format"];
        
        unlink($file);
        
        $this->component("Media")->delete($idMedia);
        
        return true;
        
    }

}

?>
